package com.world.rickcloudendpoint.net.server;

import com.world.rickcloudendpoint.demo.redis.RedisQueueSender;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;

/**
 * @description: 配置 netty server
 * @author: jlsong
 * @date: 2021/12/07 00:04:50
 * @version: 1.0
 */
public class ReceptionChannelInitializer extends ChannelInitializer<NioSocketChannel> {

    final RedisQueueSender queueSender;

    public ReceptionChannelInitializer(RedisQueueSender queueSender) {
        this.queueSender = queueSender;
    }

    @Override
    protected void initChannel(NioSocketChannel nioSocketChannel) throws Exception {
        //添加编解码
        nioSocketChannel.pipeline().addLast("decoder", new StringDecoder(CharsetUtil.UTF_8));
        // 添加编码器
        nioSocketChannel.pipeline().addLast("encoder", new StringEncoder(CharsetUtil.UTF_8));
        // 以某个字符进行解码
//        socketChannel.pipeline().addLast(new DelimiterBasedFrameDecoder(64*1024, Unpooled.copiedBuffer("".getBytes())));
        // 添加自定义消息内容处理器
        nioSocketChannel.pipeline().addLast(new ReceptionChannelHandler(queueSender));
    }
}
